<?
	$title="Add Offline Purchase";
	
	require("required/dbconnect.php");
	require_once("required/function.php");
	
	if(isset($_POST['btnsubmit']))
	{
		$email=$_POST['email'];
		$member_card_number=$_POST['member_card_number'];
		$receipt_number=$_POST['receipt_number'];
		$amount=$_POST['amount'];
		$is_new=$_POST['is_new'];
		$member_id=$_POST['member_id'];
		$purchase_date=$_POST['purchase_date'];
		
		if($is_new==0)
		{
			mysql_query("insert into os_offline_purchase(email,member_id,member_card_number,receipt_number,amount,purchase_date,created_by,created_branch_id,created_datetime)values
			('$email','$member_id','$member_card_number','$receipt_number','$amount','$purchase_date','".$_SESSION['admin_loginid']."','".$_SESSION['branch_id']."',now())")or die(mysql_error());
			header("Location:offline_purchase_view_all.php?action=1");	
						
		}else{
		
			$chk=mysql_query("select * from os_member where email='$email'")or die(mysql_error());
				
			if(mysql_num_rows($chk)>0)
			{
				header("Location:offline_purchase_add3.php?action=1");
				die();
			}
			
			$chk=mysql_query("select * from os_member where member_card_number='$member_card_number'")or die(mysql_error());
			
			if(mysql_num_rows($chk)>0)
			{
				header("Location:offline_purchase_add3.php?action=2");
				die();
			}
				
			mysql_query("insert into os_offline_purchase(email,member_id,member_card_number,receipt_number,amount,purchase_date,created_by,created_branch_id,created_datetime)values
			('$email','$member_id','$member_card_number','$receipt_number','$amount','$purchase_date','".$_SESSION['admin_loginid']."','".$_SESSION['branch_id']."',now())")or die(mysql_error());					
			
			mysql_query("insert into os_member(email,member_card_number,member_type,branch_registered_id,branch_registered_by_id,branch_registered_datetime)
						values('$email','$member_card_number','O','".$_SESSION['branch_id']."','".$_SESSION['admin_loginid']."',now())")or die(mysql_error());
			header("Location:offline_purchase_view_all.php?action=1");
		}		
	}
	
	
	require("required/header.php");
	checkAccess("allow_branch_sales_add",1);

?>
<script src="required/js/jquery-1.4.2.js"></script>
<script src="required/js/common_javascript.js"></script>
<?	
	if(empty($_GET['action']))
	{
		$msg="";
	}
	else
	{
		switch($_GET['action'])
		{
			case "1":
				$msg="<div class='dw_failed'>Email already exist!</div>";
				break;
			case "2":
				$msg="<div class='dw_failed'>Member Card Number already exist!</div>";
				break;
			case "3":
				$msg="<div class='dw_success'>Purchase Created!</div>";
				break;	
			default:
				$msg="";
				break;
		}
		
		echo "<script>displayMsg('divMsg')</script>";	
	}
		
?>	
<table width="100%" cellpadding="0" cellspacing="0" height="100%">

    <td valign="top" align="left">
       <table width="95%" align="center">
       		<tr>
            	<td>				
                	 <?= $msg ?>
                     <input type="button" value="Back" onclick="history.go(-1)" />                
                </td>  
      		</tr> 
         </table>
    </td>
</table>					
<?
	require("required/footer.php");
?>               
        